



//登录加载方法
window.onload=buttonStyle;
function buttonStyle(){
	var texts = $(".inputstyle1");
    var mark = true;
    for(var i = 0; i<texts.length; i++){
        if(texts[i].value === "" ){
            mark = false;
        }
    }
    if(mark){
      $('.login_btn p .inputbtn1').css('background','#21a1da')
    }else{
        $('.login_btn p .inputbtn1').css('background','#dadada')
    }
}



//计时器
var M=3 ;
function timedCount(){
	M--;
 	if (M == 0){
 		$('.login_btn p .inputbtn3').show();
 		clearInterval(Timer);
 		M = 3;
 	}
}

function validate(){
    var texts = $(".inputstyle1");
    var mark = true;
    for(var i = 0; i<texts.length; i++){
        if(texts[i].value === "" ){
            mark = false;
        }
    }
    if(mark){
      $('.login_btn p .inputbtn1').css('background','#21a1da')
    }else{
        $('.login_btn p .inputbtn1').css('background','#dadada')
    }
}

$(function(){
	$('#login_form').validate({
		submitHandler: function(form) {
			loginAction();
       }, 
	    errorPlacement: function(error, element){
	        var error_td = element.parent();
			error_td.addClass('erros');
	        error_td.append(error);$(error_td).click(function(){$(error_td).find('label').remove();});
	    },
        success : function(label){
			label.parent().removeClass('erros');
            label.addClass('validate_right').text('');
        },
        onkeyup: false,
        rules : {
            username : {
                required : true,
                minlength: 1,
				maxlength: 20
            },
            password : {
                required : true,
                minlength: 4,
				maxlength: 20
            }
        },
        messages : {
            username : {
                required : '请输入用户名',
				minlength: '请输入正确的用户名',
				maxlength: '请输入正确的用户名'
				
            },
            password  : {
                required : '请输入密码',
                minlength: '密码在4-20个字符之间',
				maxlength: '密码在4-20个字符之间'
            }
        }
    });
	$('.rember_part label').click(function(){
		 if($(this).hasClass('checked')){
            $(this).toggleClass('checked');
			$(this).parent().find('input').removeAttr("checked");	
         }else{
            $(this).toggleClass('checked');
            $(this).parent().find('input').attr('checked','checked');
	}});
});

function loginAction(){
	//form.action = "${pageContext.request.contextPath}/login/login.do";
	var userName = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	var checked = document.getElementById("checkebox").checked; 
	jQuery.ajax({
		url : rootPath + "/login/login.do",
		async : false,
		dataType : "json",
		type : "post",
		data : {
			username : userName,
			password : password,
			checked : checked
		},
		success : function(result) {
			if (result.code == 1) {
				$('.login_btn p .inputbtn1').hide();
				$('.login_btn p .inputbtn3').show();
				window.location.href = rootPath + "/login/success.do";
			}else if (result.code == -1){
				$("p[id='msg']").text("用户名或密码错误，请重新输入！");
				$("#LoginFail").attr("onclick","hideOrShow();");
				$('#delbox').toggle();
			}
		},
		fail : function() {
			alert("请求失败!");
		}
	});
}

function hideOrShow(){
	$('#delbox').toggle();
}



$(document).ready(function(){
    //记住密码功能
    var str = getCookie("loginInfo");
    str = str.substring(1,str.length-1);
    var username = str.split(",")[0];
    var password = str.split(",")[1];
    //自动填充用户名和密码
    $("#username").val(username);
    $("#password").val(password);
});

//获取cookie中的内容
function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') 
	    	{
	    		c = c.substring(1);
	    	}
	    if (c.indexOf(name) != -1) {
	    	return c.substring(name.length, c.length);
	    }
	}
	return "";
}

//记住密码功能
/*function remember(){
	var checked = document.getElementById("checkebox").checked; 
	alert(checked);
	//如果选中设置remFlag为1
	if(checked==true){ 
		document.getElementById("remFlag").value = "1";
	    //cookie存用户名和密码,回显的是真实的用户名和密码,存在安全问题.
	    var conFlag = confirm("记住密码容易泄漏密码，您确定要使用此功能吗?");
	    //确认标志
	    if(conFlag){ 
	    	document.getElementById("remFlag").value = 1;
	    }else{
	        $("input[type='checkbox']").removeAttr('checked');
	        document.getElementById("remFlag").value = "";
	    }
	  //如果没选中设置remFlag为""
	}else{ 
		document.getElementById("remFlag").value = "0";
	}
}*/